package com.neusoft.EYSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.EYSystem.dao.BedDAO;
import com.neusoft.EYSystem.dao.BedUsageDAO;
import com.neusoft.EYSystem.dao.ClientDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.BedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BedServiceImpl implements BedService {
    @Autowired
    private BedDAO bedDAO;

    @Autowired
    private ClientDAO clientDAO;

    @Autowired
    private BedUsageDAO bedUsageDAO;

    @Override
    public ResponseBean<HashMap<String, Integer>> getBedNum(String floorNumber) {
        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();
        if (floorNumber != null && !floorNumber.isEmpty()) {
            queryWrapper.eq("floor_number", floorNumber);
        }else{
            queryWrapper.isNotNull("floor_number"); // 如果没有提供楼层号，则查询所有床位
        }
        int totalBeds = bedDAO.selectList(queryWrapper).size();
        if( totalBeds == 0) {
            return new ResponseBean<>(500, "当前楼层暂时没有床位");
        }
        int freeNum = bedDAO.selectList(new QueryWrapper<Bed>().eq("floor_number", floorNumber).eq("status", "free")).size();
        int occupiedNum = bedDAO.selectList(new QueryWrapper<Bed>().eq("floor_number", floorNumber).eq("status", "occupied")).size();
        int outwardNum = bedDAO.selectList(new QueryWrapper<Bed>().eq("floor_number", floorNumber).eq("status", "outward")).size();

        HashMap<String, Integer> bedNumMap = new HashMap<>(
                Map.of(
                        "totalBeds", totalBeds,
                        "freeNum", freeNum,
                        "occupiedNum", occupiedNum,
                        "outwardNum", outwardNum
                )
        );
        return new ResponseBean<>(200, "查询成功", bedNumMap);
    }

    @Override
    public ResponseBean<BedInfo> getBedInfo(Integer bedId) {
        Bed bed = bedDAO.selectById(bedId);
        BedInfo bedInfo = new BedInfo();
        if(bed != null){
            bedInfo.setBed(bed);
            if("free".equals(bed.getStatus())) {
                bedInfo.setClient(null);
                bedInfo.setBedUsage(null);
            }else{
                QueryWrapper<BedUsage> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("bed_id", bedId)
                            .eq("status", "使用中");
                // TODO
                // 逻辑问题 看不见的信息维护问题
                BedUsage bedUsage = bedUsageDAO.selectOne(queryWrapper);
                if(bedUsage != null) {
                    bedInfo.setBedUsage(bedUsage);
                    Client client = clientDAO.selectById(bedUsage.getClientId());
                    if(client != null) {
                        bedInfo.setClient(client);
                    } else {
                        return new ResponseBean<>(400, "未找到对应的老人信息");
                    }
                } else {
                    return new ResponseBean<>(400, "未找到对应的床位记录信息");
                }
            }
        }else{
            return new ResponseBean<>(400, "未找到对应的床位信息");
        }
        return new ResponseBean<>(200, "查询成功", bedInfo);
    }
}
